package com.own.component.store.core.handler;

import com.own.component.common.model.ResultModel;
import com.own.component.store.core.exception.ConfirmException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * ConfirmExceptionHandler
 *
 * @author chenxueli
 * @date 2024-05-16 16:38:58
 */
@Slf4j
@Order(1)
@RestControllerAdvice
public class ConfirmExceptionHandler {

    /**
     * 二次确认异常
     *
     * @param e 异常信息
     * @return 返回信息
     */
    @ExceptionHandler(ConfirmException.class)
    public ResultModel<Map<String, Object>> handleConfirmException(ConfirmException e) {
        var resultModel = new ResultModel<Map<String, Object>>();
        var map = new HashMap<String, Object>(8);
        map.put("token", e.getToken());
        if (StringUtils.isNotEmpty(e.getKey())) {
            map.put("key", e.getKey());
        }
        resultModel.setData(map);
        resultModel.setMessage(e.getMessage());
        resultModel.setCode(e.getCode());
        return resultModel;
    }

}
